//
// Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
//
// GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
//  Author: Shawn.Tan
//  Date : 2025.10.28
//
//  History : Initial Version 2025.10.28
//
//
#include "VmInfo.h"

#include <sstream>

#include "Generator.h"
#include "Log.h"
#include "Register.h"

using namespace std;

/*!
  \file VmInfo.cc
  \breif VmInfo code for VmMapper lookup etc.
*/

namespace Green {

  void VmInfo::SetBoolAttribute(EVmInfoBoolType attrType, bool set)
  {
    EVmInfoBoolTypeBaseType attr_val = EVmInfoBoolTypeBaseType(attrType);
    if (set) {
      mBoolAttributes |= attr_val;
    }
    else {
      mBoolAttributes &= ~attr_val;
    }
    mBoolMask |= attr_val;
  }

  uint32 VmInfo::GetRegisterFieldState(const Register* pReg, EVmInfoBoolType attrType)
  {
    auto field_name = EVmInfoBoolType_to_string(attrType);
    auto reg_field = pReg->RegisterFieldLookup(field_name);
    uint32 field_value = reg_field->FieldValue();
    SetBoolAttribute(attrType, field_value);
    return field_value;
  }

  void VmInfo::GetOtherStates(const Generator& rGen)
  {
    EVmInfoBoolTypeBaseType test_bit = EVmInfoBoolTypeBaseType(1);
    auto reg_file = rGen.GetRegisterFile();

    for (EVmInfoBoolTypeBaseType i = 0; i < EVmInfoBoolTypeSize; ++ i) {
      if ((mBoolMask & test_bit) != test_bit) {
        // the bit has NOT been set.
        EVmInfoBoolType attr_type = EVmInfoBoolType(test_bit);
        auto reg_name = GetRegisterNameForField(attr_type, rGen);
        // << "getting other state for " << EVmInfoBoolType_to_string(attr_type) << " reg name " << reg_name << endl;
        auto reg_ptr = reg_file->RegisterLookup(reg_name);
        GetRegisterFieldState(reg_ptr, attr_type);
      }
      test_bit <<= 1;
    }
  }
}
